class CustomersController < ApplicationController
  filter_access_to :all
  
  def new
    # questo metodo deve mostrare un form per riempire i campi del cliente    
    @customer = Customer.new
    @customer.account = Account.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @customer }
    end
  end

  def create
    @customer = Customer.new(params[:customer])
    @customer.account = Account.new(params[:account])

    respond_to do |format|
      if @customer.save
        flash[:notice] = 'Customer was successfully created.'
        format.html { redirect_to root_path }
        format.xml  { render :xml => @customer, :status => :created, :location => @customer }
      else
        @customer.errors = @customer.account.errors
        format.html { render :action => "new" }
        format.xml  { render :xml => @customer.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def edit
    @customer = @current_account.user
  end

  def update
    respond_to do |format|
      if @current_account.user.update_attributes(params[:customer]) and
          @current_account.update_attributes(params[:account])
        flash[:notice] = 'il tuo profilo è stato modificato.'
        format.html { redirect_to root_path }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @customer.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @customer = @current_account.user
    if @customer.destroy
      flash[:notice] = "Il tuo account è stato eliminato dal sistema."
    else
      flash[:error] = "Errore di sistema"
    end
    redirect_to root_path
  end
end
